package com.wuli.food.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuli.food.common.utils.StringUtils;
import com.wuli.food.dto.TypeDto;
import com.wuli.food.entity.FoodType;
import com.wuli.food.mapper.FoodTypeMapper;
import com.wuli.food.service.FoodTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuli.food.vo.TypeAddVo;
import com.wuli.food.vo.TypeSearchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiongqiaoqiao
 * @since 2023-05-18
 */
@Service
public class FoodTypeServiceImpl extends ServiceImpl<FoodTypeMapper, FoodType> implements FoodTypeService {

    @Override
    public String add(TypeAddVo addVo) {
        FoodType type = new FoodType();
        BeanUtils.copyProperties(addVo, type);
        type.setIsDeleted(0);
        if(StringUtils.isNotEmpty(addVo.getId())){
            this.updateById(type);
        }else {
            this.save(type);
        }
        return type.getId();
    }

    @Override
    public IPage<TypeDto> search(TypeSearchVo searchVo) {
        IPage iPage = new Page(searchVo.getCurrent(), searchVo.getSize());
        IPage<TypeDto> page = baseMapper.search(searchVo, iPage);
        return page;
    }

    @Override
    public TypeDto getDetail(String id) {
        TypeDto dto = baseMapper.getDetail(id);
        return dto;
    }

    @Override
    public void deleteById(String id) {
        FoodType byId = this.getById(id);
        byId.setIsDeleted(1);
        this.updateById(byId);
    }
}
